/******************************************************************************* 
 * Copyright (c) 2008 xored software, Inc.  
 * 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 *     xored software, Inc. - initial API and Implementation (Yuri Strot) 
 *******************************************************************************/
package com.xored.glance.tests.core;

import junit.framework.TestCase;

import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

import com.xored.glance.core.StyleList;

/**
 * @author Yuri Strot
 * 
 */
public class StyleListTest extends TestCase {

	private Display display;
	private Color color1;
	private Color color2;
	private Color color3;

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		display = new Display();
		color1 = new Color(display, 255, 0, 0);
		color2 = new Color(display, 0, 255, 0);
		color3 = new Color(display, 0, 0, 255);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		color1.dispose();
		color2.dispose();
		color3.dispose();
		display.dispose();
	}

	public void testApplyStyles() throws Exception {
		StyleRange range11 = new StyleRange(1, 4, color1, null);
		StyleRange range12 = new StyleRange(7, 2, color2, null);
		StyleRange range13 = new StyleRange(9, 2, color1, null);
		StyleRange range14 = new StyleRange(13, 1, color3, null);
		StyleRange range15 = new StyleRange(15, 2, color1, null);
		StyleRange range16 = new StyleRange(18, 1, color1, null);

		StyleList list1 = new StyleList(new StyleRange[] { range11, range12,
				range13, range14, range15, range16 });

		StyleRange range21 = new StyleRange(2, 2, color3, null);
		StyleRange range22 = new StyleRange(6, 3, color3, null);
		StyleRange range23 = new StyleRange(12, 1, color3, null);
		StyleRange range24 = new StyleRange(15, 5, color3, null);

		StyleList list2 = new StyleList(new StyleRange[] { range21, range22,
				range23, range24 });

		StyleRange range31 = new StyleRange(1, 1, color1, null);
		StyleRange range32 = new StyleRange(2, 2, color3, null);
		StyleRange range33 = new StyleRange(4, 1, color1, null);
		StyleRange range34 = new StyleRange(6, 3, color3, null);
		StyleRange range35 = new StyleRange(9, 2, color1, null);
		StyleRange range36 = new StyleRange(12, 2, color3, null);
		StyleRange range37 = new StyleRange(15, 5, color3, null);

		StyleList list3 = new StyleList(new StyleRange[] { range31, range32,
				range33, range34, range35, range36, range37 });

		StyleList result = list1.applyStyles(list2);
		assertEquals(list3, result);
	}

	public void testSubStyles() throws Exception {
		StyleRange range1 = new StyleRange(2, 2, color1, null);
		StyleRange range2 = new StyleRange(6, 3, color2, null);

		StyleRange range3 = new StyleRange(3, 1, color1, null);
		StyleRange range4 = new StyleRange(6, 2, color2, null);

		StyleList list = new StyleList(new StyleRange[] { range1, range2 });

		StyleList empty = new StyleList();
		StyleList list2 = new StyleList(range3);
		StyleList list3 = new StyleList(new StyleRange[] { range3, range4 });

		StyleList result1 = list.subStyles(1, 2);
		assertEquals(result1, empty);

		StyleList result2 = list.subStyles(3, 5);
		assertEquals(result2, list2);

		StyleList result3 = list.subStyles(3, 8);
		assertEquals(result3, list3);

		StyleList result4 = list.subStyles(2, 10);
		assertEquals(result4, list);

		StyleList result5 = list.subStyles(4, 6);
		assertEquals(result5, empty);
	}
}
